package com.weimin.SportAppBackend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weimin.SportAppBackend.entity.User;
import com.weimin.SportAppBackend.entity.dto.BindPhoneDTO;
import com.weimin.SportAppBackend.entity.dto.PhoneLoginDTO;
import com.weimin.SportAppBackend.entity.dto.UserQueryDTO;
import com.weimin.SportAppBackend.entity.dto.UserStatusDTO;
import com.weimin.SportAppBackend.entity.vo.LoginVO;
import com.weimin.SportAppBackend.service.UserService;
import com.weimin.SportAppBackend.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author : huiMing
 * Date : 2025年06月23日 15:04
 * @version V1.0
 */
@Tag(name = "UserController", description = "用户管理")
@RestController
@RequestMapping("/auth")
public class UserController {

    @Resource
    private UserService userService; // 注入用户业务服务

    /**
     * 处理微信登录请求
     * @param code 微信登录请求code
     * @return 包含JWT Token和用户信息的响应
     */
    @Operation(summary = "微信登录")
    @PostMapping("/wx/openid")
    public R<LoginVO> wechatLogin(@RequestBody String code) {
        try {
            LoginVO response = userService.wechatLogin(code);
            return R.ok("登录成功", response);
        } catch (RuntimeException e) {
            // 微信登录失败，返回错误信息
            return R.fail(e.getMessage());
        }
    }



    /**
     * 手机号绑定接口
     * @param bindPhoneDTO 绑定信息
     * @param userId 用户ID（从JWT Token中获取）
     * @return 绑定结果
     */
    @Operation(summary = "绑定手机号")
    @PostMapping("/bind-phone")
    public R<Boolean> bindPhone(@RequestBody BindPhoneDTO bindPhoneDTO,
                                @RequestAttribute("userId") Integer userId) {
        try {
            boolean result = userService.bindPhone(userId, bindPhoneDTO.getPhone(), bindPhoneDTO.getCode());
            return R.ok("绑定成功", result);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 手机号登录注册接口
     * @param phoneLoginDTO 登录信息
     * @return 登录结果
     */
    @Operation(summary = "手机号登录注册")
    @PostMapping("/phone/login")
    public R<LoginVO> phoneLogin(@RequestBody PhoneLoginDTO phoneLoginDTO) {
        try {
            LoginVO response = userService.phoneLogin(
                    phoneLoginDTO.getPhone(),
                    phoneLoginDTO.getCode(),
                    phoneLoginDTO.getPassword()
            );
            return R.ok("登录成功", response);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }





    // ------------------------  用户管理 ----------------------------------------
    /**
     * 查询用户列表
     * @param queryDTO 查询条件
     * @return 用户列表
     */
    @Operation(summary = "查询用户列表")
    @GetMapping("/admin/list")
    public R<IPage<User>> getUserList(UserQueryDTO queryDTO) {
        try {
            IPage<User> userPage = userService.getUserList(queryDTO);
            return R.ok("查询成功", userPage);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 修改用户状态
     * @param statusDTO 状态信息
     * @return 修改结果
     */
    @Operation(summary = "修改用户状态")
    @PutMapping("/admin/status")
    public R<Boolean> updateUserStatus(@RequestBody UserStatusDTO statusDTO) {
        try {
            boolean result = userService.updateUserStatus(statusDTO);
            return R.ok("状态修改成功", result);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

    /**
     * 删除用户（逻辑删除）
     * @param id 用户ID
     * @return 删除结果
     */
    @Operation(summary = "删除用户")
    @DeleteMapping("/admin/{id}")
    public R<Boolean> deleteUser(@PathVariable Integer id) {
        try {
            boolean result = userService.deleteUser(id);
            return R.ok("删除成功", result);
        } catch (RuntimeException e) {
            return R.fail(e.getMessage());
        }
    }

}
